package flc.ast.fragment;

import android.graphics.Color;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import flc.ast.AppConfig;
import flc.ast.R;
import flc.ast.activity.WallpaperActivity;
import flc.ast.activity.WallpaperDetailActivity;
import flc.ast.adapter.HomeWallpaperAdapter;
import flc.ast.databinding.FragmentHomeBinding;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.base.BaseNoModelFragment;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkResBean;
import stark.common.bean.StkTagResBean;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link HomeFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class HomeFragment extends BaseNoModelFragment<FragmentHomeBinding> {

    private HomeWallpaperAdapter wallpaperAdapter;
    private int page = 1;
    private int refreshTime = 200;
    private int type;
    private List<StkTagResBean> listType1 = new ArrayList<>();
    private List<StkTagResBean> listType2 = new ArrayList<>();

    @Override
    protected int onCreate() {
        return R.layout.fragment_home;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent1(getActivity(), mDataBinding.event1Container);
        EventStatProxy.getInstance().statEvent5(getActivity(), mDataBinding.event5Container);

        mDataBinding.ivType1.setOnClickListener(this);
        mDataBinding.ivType2.setOnClickListener(this);
        mDataBinding.ivType3.setOnClickListener(this);
        mDataBinding.tvType4.setOnClickListener(this);
        mDataBinding.tvType5.setOnClickListener(this);
        mDataBinding.tvType6.setOnClickListener(this);

        mDataBinding.rvWallpaperList.setLayoutManager(new GridLayoutManager(mContext, 2));
        wallpaperAdapter = new HomeWallpaperAdapter();
        mDataBinding.rvWallpaperList.setAdapter(wallpaperAdapter);
        wallpaperAdapter.setOnItemClickListener(this);

        mDataBinding.refreshWallpaperItem.setRefreshHeader(new ClassicsHeader(mContext));
        mDataBinding.refreshWallpaperItem.setRefreshFooter(new ClassicsFooter(mContext));
        mDataBinding.refreshWallpaperItem.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                page++;
                getData(listType2.get(type).getHashid());
                mDataBinding.refreshWallpaperItem.finishLoadMore(refreshTime);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                page = 1;
                getData(listType2.get(type).getHashid());
                mDataBinding.refreshWallpaperItem.finishRefresh(refreshTime);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.ivType1://动漫
                WallpaperActivity.stkTagResBean = listType1.get(0);
                startActivity(WallpaperActivity.class);
                break;
            case R.id.ivType2://游戏
                WallpaperActivity.stkTagResBean = listType1.get(1);
                startActivity(WallpaperActivity.class);
                break;
            case R.id.ivType3://风景
                WallpaperActivity.stkTagResBean = listType1.get(2);
                startActivity(WallpaperActivity.class);
                break;
            case R.id.tvType4://热门
                clearSelection();
                mDataBinding.tvType4.setTextColor(Color.parseColor("#181818"));
                mDataBinding.ivType4Sel.setVisibility(View.VISIBLE);
                page = 1;
                type = 0;
                getData(listType2.get(0).getHashid());
                break;
            case R.id.tvType5://最新
                clearSelection();
                mDataBinding.tvType5.setTextColor(Color.parseColor("#181818"));
                mDataBinding.ivType5Sel.setVisibility(View.VISIBLE);
                page = 1;
                type = 1;
                getData(listType2.get(1).getHashid());
                break;
            case R.id.tvType6://精选
                clearSelection();
                mDataBinding.tvType6.setTextColor(Color.parseColor("#181818"));
                mDataBinding.ivType6Sel.setVisibility(View.VISIBLE);
                page = 1;
                type = 2;
                getData(listType2.get(2).getHashid());
                break;
        }
    }

    @Override
    protected void onItemClickCallback(@NonNull BaseQuickAdapter<?, ?> adapter, @NonNull View view, int position) {
        WallpaperDetailActivity.wallpaperBean = wallpaperAdapter.getItem(position);
        startActivity(WallpaperDetailActivity.class);
    }

    @Override
    protected void initData() {
        getType1Data();
        getType2Data();
    }

    private void getType1Data() {
        StkResApi.getChildTagList(this, AppConfig.TYPE1_DATA, StkResApi.createParamMap(0, 3), true, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listType1.addAll(result);
                    }
                }
            }
        });
    }

    private void getType2Data() {
        StkResApi.getChildTagList(this, AppConfig.TYPE2_DATA, StkResApi.createParamMap(0, 3), true, new IReqRetCallback<List<StkTagResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkTagResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        listType2.addAll(result);
                        getData(listType2.get(0).getHashid());
                    }
                }
            }
        });
    }

    private void getData(String hashid) {
        StkResApi.getTagResourceList(this, AppConfig.URL_PREFIX + hashid, StkResApi.createParamMap(0, 10), false, new IReqRetCallback<List<StkResBean>>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable List<StkResBean> result) {
                if (success) {
                    if (result != null && result.size() > 0) {
                        if (page == 1) {
                            wallpaperAdapter.setList(result);
                        } else {
                            wallpaperAdapter.addData(result);
                        }
                    }
                }
            }
        });
    }

    private void clearSelection() {
        mDataBinding.tvType4.setTextColor(Color.parseColor("#80181818"));
        mDataBinding.ivType4Sel.setVisibility(View.GONE);
        mDataBinding.tvType5.setTextColor(Color.parseColor("#80181818"));
        mDataBinding.ivType5Sel.setVisibility(View.GONE);
        mDataBinding.tvType6.setTextColor(Color.parseColor("#80181818"));
        mDataBinding.ivType6Sel.setVisibility(View.GONE);
    }

}